package com.lbz.controller.front;

import com.lbz.common.Constant;
import com.lbz.common.ResponseCode;
import com.lbz.common.ServerResponse;
import com.lbz.dao.OrderMapper;
import com.lbz.entity.User;
import com.lbz.service.PayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 支付控制层
 *
 * @Author 林必昭
 * @Date 2019/6/27 13:55
 */

@Controller
@RequestMapping("/payInfo/")
public class PayInfoController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private PayInfoService payInfoService;

    /**
     * 支付
     *
     * @param session
     * @param orderNo
     * @param request
     * @return
     */
    @RequestMapping("pay.do")
    @ResponseBody
    public ServerResponse pay(HttpSession session, Long orderNo, HttpServletRequest request) {
        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        String path = request.getSession().getServletContext().getRealPath("upload");   //path拿到后面没有斜线
        //支付逻辑实现
        return payInfoService.pay(user.getId(), orderNo, path);
    }

    /**
     * 查询订单支付状态
     * success
     * {
     * "status"：0，
     * "data":true
     * }
     * fail
     * {
     * "status":1
     * "msg":"该用户没有该订单，查询无效"
     * }
     * <p>
     * 与前端的约定是返回Boolean类型
     *
     * @param session
     * @param orderNo
     * @return
     */
    @RequestMapping("query_order_pay_status.do")
    @ResponseBody
    public ServerResponse<Boolean> queryOrderByPayStatus(HttpSession session, Long orderNo) {
        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        ServerResponse serverResponse = payInfoService.queryOrderPayStatus(user.getId(), orderNo);
        if (serverResponse.isSuccess()) {
            return ServerResponse.createBySuccess(true);
        }
        return ServerResponse.createBySuccess(false);
    }
}
